﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters
{
    public class Func_Impacts
    {
        public Func_Impacts()
        {
            dataContext = new DSSEntities();
        }

        DSSEntities dataContext;


        public List<Impacts> getAllImpacts()
        {

            IQueryable<Impacts> Query =
                  from c in dataContext.Impacts
                  select c;

            List<Impacts> Impacts = Query.ToList();

            return Impacts;
        }

        public List<string> getComponentMainCat(string project)
        {
            List<string> complist = new List<string>();
            IQueryable<string> quary = (from p in dataContext.Components
                                        join b in dataContext.Impacts on p.cmpt_reference equals b.impt_component
                                        join c in dataContext.ProjectImpacts on b.impt_reference equals c.impt_reference
                                        where
                                        c.proj_reference.Contains(project)

                                        select p.cmpt_name).Distinct();
            complist = quary.ToList();

            return complist;

        }


        public List<Impacts> getImpactsFromProjectImpacts(String prefe, String impcName)
        {
            //var innerJoinQuery =
            //from c in cecbContext.ProjectImpacts
            //join d in cecbContext.Impacts on c.impt_reference equals d.impt_reference
            //where c.proj_reference.Contains(prefe) && d.impt_name.Contains(impcName)
            //select new { CustomerName = d.impt_name };
            List<Impacts> SelectedImpacts = null;
            try
            {
                // String xim = dataContext.Impacts.First(i => i.impt_name.Contains(impcName)).impt_reference;
                IQueryable<Impacts> query = from c in dataContext.ProjectImpacts
                                            join b in dataContext.Impacts on c.impt_reference equals b.impt_reference
                                            //  join d in dataContext.LeopoldMatrixValues on b.impt_reference equals d.impt_reference
                                            where c.proj_reference == prefe && b.impt_name.Contains(impcName)
                                            //&& d.mtrx_magnitude == null && d.mtrx_importance == null
                                            select b;




                SelectedImpacts = query.ToList();
            }
            catch (Exception)
            {


            }


            return SelectedImpacts;
        }



        public List<ProjectImpacts> getProjectImpactsByProjectID(String impcName)
        {
            IQueryable<ProjectImpacts> Query =
                  from c in dataContext.ProjectImpacts
                  where c.proj_reference.Contains(impcName)

                  select c;

            List<ProjectImpacts> SelectedImpacts = Query.ToList();

            return SelectedImpacts;
        }




        public List<Impacts> getImpactDetailsByName(String name)
        {

            IQueryable<Impacts> Query =
                  from c in dataContext.Impacts
                  where c.impt_name.Contains(name)
                  select c;

            List<Impacts> SelectedComponent = Query.ToList();

            return SelectedComponent;

        }

        private String getImpactReference()
        {
            int sub;


            try
            {
                var idKey = (from c in dataContext.Impacts
                             orderby c.impt_id descending
                             select c).First();


                String id = idKey.impt_reference.ToString();

                sub = int.Parse(id.Substring(4)) + 1;
            }
            catch (Exception)
            {
                sub = 1;
            }
            String newId = "IMPC" + sub;
            return newId.ToString();

        }


        Impacts impt;
        public bool InsertImpactsEditedMember()
        {
            bool IsInserted = false;
            try
            {
                String reference = getImpactReference();
                impt = new Impacts();
                impt.impt_reference = reference;
                //      impt.impt_name = name;
                //    impt.Component.cmpt_reference = designation;
                impt.impt_status = true;


                dataContext.AddToImpacts(impt);
                dataContext.SaveChanges();



                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }




        public bool InsertImpact(List<String> phase, List<String> act, int count, String proRef)
        {
            Impacts impac;
            int itemNo = 0;
            bool IsInserted = false;
            try
            {
                while (count > 0)
                {

                    //cecbContext.Impacts.First(i => i.impt_name == phase[itemNo]).;

                    String reference = getImpactReference();
                    impac = new Impacts();
                    impac.impt_reference = reference;
                    impac.impt_name = phase[itemNo];
                    String cname = act[itemNo];
                    impac.ComponentsReference.Value = dataContext.Components.First(i => i.cmpt_name == cname);

                    impac.impt_status = true;
                    itemNo++;
                    count--;
                    dataContext.AddToImpacts(impac);
                    dataContext.SaveChanges();

                    ProjectImpacts pimpact = new ProjectImpacts();
                    pimpact.proj_reference = proRef;
                    pimpact.impt_reference = reference;
                    pimpact.impt_addedBy = "Admin";
                    pimpact.impt_approvedBy = "";
                    pimpact.projImpact_status = true;
                    dataContext.AddToProjectImpacts(pimpact);
                    dataContext.SaveChanges();


                }


                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }


        public String findIref(String imp)
        {


            string sub = "";



            try
            {
                var idKey = (from c in dataContext.Impacts
                             where c.impt_name.Contains(imp)
                             select c).First();


                sub = idKey.impt_reference.ToString();


            }
            catch (Exception)
            {

            }

            return sub;


        }


        public String getInpactNameById(String id)
        {

            var idKey = (from c in dataContext.Impacts
                         where c.impt_reference == id
                         select c).First();


            return idKey.impt_name.ToString();

        }

        public List<Impacts> getImpacts(string project, string comp)
        {
            List<Impacts> list = new List<Impacts>();
            IQueryable<Impacts> quary = (from p in dataContext.Impacts
                                         join b in dataContext.ProjectImpacts

                                         on p.impt_reference
                                         equals b.impt_reference
                                         join d in dataContext.Components on p.impt_component equals d.cmpt_reference
                                         where
                                         b.proj_reference.Contains(project) && d.cmpt_name.Contains(comp)

                                         select p);
            list = quary.ToList();


            // (from a in context.Accounts
            // join c in context.Clients on a.UserID equals c.UserID
            // where c.ClientID == yourDescriptionObject.ClientID
            // select a.Balance)
            //.SingleOrDefault();

            //(from c in ce.Campaign.Include("Test")
            // join t in ce.Tests.Include("TestAnswer").Include("User") on c.Test.ID equals t.ID
            // where c.ID == campaignID
            // select c).FirstOrDefault();




            list = quary.ToList();
            return list;
        }
        public bool updateImpactsDetails(string impt_ref, string impact)
        {
            bool isselected = false;
            try
            {

                Impacts impactObject = dataContext.Impacts.First(i => i.impt_reference == impt_ref);

                //    ProjectImpacts projectimpactObject = dataContext.ProjectImpacts.First(i => i.ImpactsReference == impt_ref);

                impactObject.impt_name = impact;


                dataContext.SaveChanges();
                isselected = true;

            }
            catch (Exception)
            {

            }

            return isselected;

        }

        public String findAref(String act)
        {


            string sub = "";



            try
            {
                var idKey = (from c in dataContext.Activities
                             where c.actv_name.Contains(act)
                             select c).First();


                sub = idKey.actv_reference.ToString();


            }
            catch (Exception)
            {

            }

            return sub;


        }





    }
}
